$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'sys/tcmeetingroomapplicant/list',
        datatype: "json",
        colModel: [
			//{ label: '会员申请者id', name: 'applicantId', index: 'APPLICANT_ID', width: 80 },
			//{ label: '审批人id', name: 'approvalId', index: 'APPROVAL_ID', width: 80 },
			//{ label: '申请人类型（SYSTEM:系统;MEMBER:会员）', name: 'applicantType', index: 'APPLICANT_TYPE', width: 80 },
			//{ label: '会员OPENID', name: 'openid', index: 'OPENID', width: 80 },
			//{ label: '会议室ID', name: 'meetingRoomId', index: 'MEETING_ROOM_ID', width: 80 },
			{ label: '会议中心ID', name: 'conventionCenterId', index: 'CONVENTION_CENTER_ID', width: 80 },
			{ label: '申请说明', name: 'applicantMemo', index: 'APPLICANT_MEMO', width: 80 },

			{ label: '会议中心名称', name: 'conventionCenterName', index: 'CONVENTION_CENTER_NAME', width: 80 },
			{ label: '会议中心位置', name: 'position', index: 'POSITION', width: 80 },
			{ label: '会议室名称', name: 'meetingRoomName', index: 'MEETING_ROOM_NAME', width: 80 },
			{ label: '会议室状态', name: 'meetingRoomStatus', index: 'MEETING_ROOM_STATUS', width: 80,formatter: function(value, options, row){
					if(value == "NORMAL"){
						return '<span class="label label-success">正常</span>'
					}else if(value == "FORBIDDEN"){
						return '<span class="label label-danger">禁用</span>'
					}} },
			{ label: '会议室容量', name: 'capacity', index: 'CAPACITY', width: 80 },
			//{ label: '会议中心坐标', name: 'coordinate', index: 'COORDINATE', width: 80 },
			{ label: '会议室描述', name: 'meetingRoomMemo', index: 'MEETING_ROOM_MEMO', width: 80 },
			{ label: '会员参加者', name: 'participatorCount', index: 'PARTICIPATOR_COUNT', width: 80 },

			//{ label: '会议室硬件配套', name: 'hardWareMemo', index: 'HARD_WARE_MEMO', width: 80 },

			{ label: '申请状态', name: 'applicantState', index: 'APPLICANT_STATE', width: 80,formatter: function(value, options, row){
					if(value == "UN_AUDIT"){
						return '<span class="label label-success">申请待审核</span>'
					}else if(value == "APPLY_CANCEL"){
						return '<span class="label label-danger">申请取消</span>'
					}else if(value == "AUDIT_PASS"){
						return '<span class="label label-danger">申请审核通过</span>'
					}else if(value == "AUDIT_FAIL"){
						return '<span class="label label-danger">申请审核不通过</span>'
					}else if(value == "USING"){
						return '<span class="label label-danger">会议室使用中</span>'
					}else if(value == "USE_FINISHED"){
						return '<span class="label label-danger">会议室使用结束</span>'
					}}
					},
			{ label: '审批意见', name: 'approvalOpinion', index: 'APPROVAL_OPINION', width: 80 },
			{ label: '使用开始时间', name: 'useStartTime', index: 'USE_START_TIME', width: 80 }, 			
			{ label: '使用结束时间', name: 'useEndTime', index: 'USE_END_TIME', width: 80 }, 			
			//{ label: '申请时间', name: 'applicantTime', index: 'APPLICANT_TIME', width: 80 },
			//{ label: '审批时间', name: 'approvalTime', index: 'APPROVAL_TIME', width: 80 },
			//{ label: '创建时间', name: 'createTime', index: 'CREATE_TIME', width: 80 },
			//{ label: '修改时间', name: 'updateTime', index: 'UPDATE_TIME', width: 80 },
			//{ label: '备注', name: 'remark', index: 'REMARK', width: 80 }
        ],
		viewrecords: true,
        height: 580,
        rowNum: 50,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });

    $("#maxroomppeople").blur(function () {

		if(parseInt($("#maxroomppeople").val())>parseInt
		($("#capacity").val())){
			$("#maxroomppeople").val("0");
			alert("最大人数不能超过会议室最大容量人数");

		}
	});
});




var vm = new Vue({
	el:'#rrapp',
	data:{
		showList: true,
		title: null,
		tcMeetingRoomApplicant: {conventionCenterName:null,conventionCenterId:null,meetingroomName:null,capacity:null},
		},
	methods: {

		showConventcenter: function(event){

			layer.open({
				type: 2,
				offset: '50px',
				skin: 'layui-layer-molv',
				title: "选择会议中心",
				area: ['1100px', '500px'],
				shade: 0,
				shadeClose: true,
				content: 'http://localhost:8080/renren-admin/modules/sys/tcconventioncenter_load.html',
				btn: ['确定', '取消'],
				btn1: function (index,layero) {
					var ibody = layer.getChildFrame ('body',index); //获取iframe页面body
					var tr = ibody.find("#jqGrid tr[class*='success']");
					var conventionCenterName = tr.find("td:eq(3)").text();
					var conventionCenterId = tr.find("td:eq(2)").text();
					var status = tr.find("td:eq(5)").text();
					if(tr.length>1){
						alert("只能选择一个会议中心");
						return;
					} else if(tr.length==0){
						alert("请选择一个会议中心");
						return;
					}
					var sta = "禁用"
					if (status==sta){
						alert("请勿选择禁止的会议中心");
						return;
					}
					/*alert("id："+conventionCenterId+",会议中心名称:"+conventionCenterName);*/
					vm.tcMeetingRoomApplicant.conventionCenterName=conventionCenterName;
					vm.tcMeetingRoomApplicant.conventionCenterId=conventionCenterId;
					$("#conventionCenterName").val(conventionCenterName);
					$("#conventionCenterId").val(conventionCenterId);
					layer.close(index);
				}
			});
		},
		showMeetingroom: function(event){
			var hyzx = $("#conventionCenterName").val();
			layer.open({
				type: 2,
				offset: '50px',
				skin: 'layui-layer-molv',
				title: "选择会议室",
				area: ['1100px', '500px'],
				shade: 0,
				shadeClose: true,
				content: 'http://localhost:8080/renren-admin/modules/sys/tcmeetingroom_load.html?zhongxin='+hyzx,
				btn: ['确定', '取消'],
				btn1: function (index,layero) {
					var ibody = layer.getChildFrame ('body',index); //获取iframe页面body
					var tr = ibody.find("#jqGrid tr[class*='success']");
					var capacity = tr.find("td:eq(5)").text();
					var meetingroomName = tr.find("td:eq(3)").text();
					if(tr.length>1){
						alert("只能选择一个会议室");
						return;
					} else if(tr.length==0){
						alert("请选择一个会议室");
						return;
					}
					var sta = "禁用"
					if (status==sta){
						alert("请勿选择禁止的会议室");
						return;
					}
					/*alert("id："+conventionCenterId+",会议中心名称:"+conventionCenterName);*/
					vm.tcMeetingRoomApplicant.meetingroomName=meetingroomName;
					vm.tcMeetingRoomApplicant.capacity=capacity;
					$("#meetingRoomName").val(meetingroomName);
					$("#capacity").val(capacity);
					layer.close(index);
				}
			});
		},
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.tcMeetingRoomApplicant = {};
		},
		update: function (event) {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";
            
            vm.getInfo(id)
		},
		saveOrUpdate: function (event) {
		    $('#btnSaveOrUpdate').button('loading').delay(1000).queue(function() {
                var url = vm.tcMeetingRoomApplicant.id == null ? "sys/tcmeetingroomapplicant/save" : "sys/tcmeetingroomapplicant/update";
                $.ajax({
                    type: "POST",
                    url: baseURL + url,
                    contentType: "application/json",
                    data: JSON.stringify(vm.tcMeetingRoomApplicant),
                    success: function(r){
                        if(r.code === 0){
                             layer.msg("操作成功", {icon: 1});
                             vm.reload();
                             $('#btnSaveOrUpdate').button('reset');
                             $('#btnSaveOrUpdate').dequeue();
                        }else{
                            layer.alert(r.msg);
                            $('#btnSaveOrUpdate').button('reset');
                            $('#btnSaveOrUpdate').dequeue();
                        }
                    }
                });
			});
		},
		del: function (event) {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}
			var lock = false;
            layer.confirm('确定要删除选中的记录？', {
                btn: ['确定','取消'] //按钮
            }, function(){
               if(!lock) {
                    lock = true;
		            $.ajax({
                        type: "POST",
                        url: baseURL + "sys/tcmeetingroomapplicant/delete",
                        contentType: "application/json",
                        data: JSON.stringify(ids),
                        success: function(r){
                            if(r.code == 0){
                                layer.msg("操作成功", {icon: 1});
                                $("#jqGrid").trigger("reloadGrid");
                            }else{
                                layer.alert(r.msg);
                            }
                        }
				    });
			    }
             }, function(){
             });
		},
		getInfo: function(id){
			$.get(baseURL + "sys/tcmeetingroomapplicant/info/"+id, function(r){
                vm.tcMeetingRoomApplicant = r.tcMeetingRoomApplicant;
            });
		},
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{ 
                page:page
            }).trigger("reloadGrid");
		}
	}
});